
package cn.com.pusai.common.action;

import cn.com.pusai.base.logger.service.LoggerService;
import cn.com.pusai.common.config.SysProperties;
import cn.com.pusai.common.model.WebInfo;
import cn.com.pusai.core.exception.ServiceException;
import cn.com.pusai.core.page.PageResult;
import com.opensymphony.xwork2.ActionSupport;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;

public class BaseAction extends ActionSupport
{

	protected LoggerService loggerService;
	private WebInfo webInfo;
	private String selectedRow[];
	private int pageNo;
	private PageResult pageResult;
	private String opType;
	private int pageSize;
	private Map searchValue;

	public BaseAction()
	{
		webInfo = new WebInfo();
		searchValue = new HashMap();
	}

	public void setLoggerService(LoggerService loggerService)
	{
		this.loggerService = loggerService;
	}

	public Map getSearchValue()
	{
		return searchValue;
	}

	public void setSearchValue(Map searchValue)
	{
		this.searchValue = searchValue;
	}

	public int getPageSize()
	{
		HttpSession session = ServletActionContext.getRequest().getSession(false);
		if (session != null && session.getAttribute("CUSTOM_PAGE_SIZE") != null)
		{
			pageSize = Integer.valueOf((String)session.getAttribute("CUSTOM_PAGE_SIZE")).intValue();
			if (pageSize == 0)
				pageSize = SysProperties.PAGESIZE;
		} else
		{
			pageSize = SysProperties.PAGESIZE;
		}
		return pageSize;
	}

	public void handleException(Exception e)
	{
		if (e instanceof ServiceException)
		{
			e.printStackTrace();
			addInfo(e.getMessage());
		} else
		{
			e.printStackTrace();
			addError(e.getMessage());
		}
		setActionStatus("failure");
	}

	public boolean isSuccess()
	{
		String error = getWebInfo().getActionStatus();
		boolean isSuccess = true;
		if (error != null && error.equals("failure"))
			isSuccess = false;
		return isSuccess;
	}

	public Long[] getSelectedRowLong()
	{
		if (selectedRow != null && selectedRow.length > 0)
		{
			Long reurnValue[] = new Long[selectedRow.length];
			for (int i = 0; i < selectedRow.length; i++)
				reurnValue[i] = Long.valueOf(Long.parseLong(selectedRow[i]));

			return reurnValue;
		} else
		{
			return null;
		}
	}

	public void addError(String error)
	{
		webInfo.addError(error);
	}

	public void addInfo(String info)
	{
		webInfo.addInfo(info);
	}

	public void setActionStatus(String actionStatus)
	{
		webInfo.setActionStatus(actionStatus);
	}

	public PageResult getPageResult()
	{
		return pageResult;
	}

	public void setPageResult(PageResult pageResult)
	{
		this.pageResult = pageResult;
	}

	public WebInfo getWebInfo()
	{
		return webInfo;
	}

	public void setWebInfo(WebInfo webInfo)
	{
		this.webInfo = webInfo;
	}

	public int getPageNo()
	{
		return pageNo;
	}

	public void setPageNo(int pageNo)
	{
		this.pageNo = pageNo;
	}

	public String[] getSelectedRow()
	{
		return selectedRow;
	}

	public void setSelectedRow(String selectedRow[])
	{
		this.selectedRow = selectedRow;
	}

	public String getOpType()
	{
		return opType;
	}

	public void setOpType(String opType)
	{
		this.opType = opType;
	}
}
